import BaseView from "../../../cfw/mvc/BaseView";
import GiftItemVIew from "./GiftItemVIew";
import GiftItemModel from "../model/GiftItemModel";
import { ItemState } from "../../../cfw/tools/Define";
import ModuleManager, { ModuleID } from "../../../cfw/module/ModuleManager";
import { ResType } from "../../../cfw/res/ResInterface";
import ResItem from "../../../cfw/res/ResItem";

// Learn TypeScript:
//  - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

const { ccclass, property } = cc._decorator;

@ccclass
export default class GiftShowView extends BaseView {

    @property(cc.Sprite)
    item: cc.Sprite = null;

    @property(cc.Node)
    top: cc.Node = null;

    @property(cc.Node)
    bottom: cc.Node = null;

    @property(cc.Node)
    light: cc.Node = null;


    protected model: GiftItemModel;

    onLoad() {
        this.setSprite(this.item,this.model.getIcon())
    }

	setSprite(sp: cc.Sprite, image: string) {
		ModuleManager.getLoader(ModuleID.GIFT).loadRes(image, ResType.SpriteFrame, (err, res: ResItem) => {
			if (err || !cc.isValid(this.node)) {
				return;
			}
			sp.spriteFrame = res.getRes()
		})
	}    
    start() {
        this.light.active = false;

        this.item.node.active = false;
    }

    onButtonClick() {
        this.model.setState(ItemState.GOT)
        this.item.node.active = true;
        this.light.active = true;
        cc.tween(this.top).by(0.5, { y: 200, opacity: 0 }).start()
        cc.tween(this.bottom).by(0.5, { y: -200, opacity: 0 }).call(() => {
            this.top.active = false;
            this.bottom.active = false;
            let audio = this.model.getAudio();
            if (audio) {
                ModuleManager.getAudio(ModuleID.GIFT).playSound(audio)
            }
            cc.tween(this).to(1, {}).call(() => {
                this.finish()
            }).start()
        }).start()

    }

    finish() {
        this.hide()
    }



    // update (dt) {}
}
